package com.snowy.framework.util;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class RandomUtil {
	/**
	 * 获取指定范围内的随机数
	 * @param min
	 * @param max
	 * @return
	 */
	public static int getRandom(int min, int max) {
		return (int) (min + Math.random() * (max - min + 1));
	}

	/**
	 * 生成制定长度的纯数字随机字符串
	 *
	 * @param length 制定的字符串长度
	 *               return String 纯数字的随机字符串
	 */
	public static final String getRandomNumString(int length) {
		Random randGen = null;
		char[] numbersAndLetters = null;

		if (length < 1) {
			return "";
		}
		if (randGen == null) {
			randGen = new Random();
			numbersAndLetters = ("0123456789").toCharArray();
		}
		char[] randBuffer = new char[length];
		for (int i = 0; i < randBuffer.length; i++) {
			randBuffer[i] = numbersAndLetters[randGen.nextInt(10)];
		}
		randGen = null;
		numbersAndLetters = null;
		return new String(randBuffer);
	}

	/**
	 * 取当前时间到秒加上随机数
	 *
	 * @param num 14位加随机数位数
	 * @return
	 */
	public static String getTimeNumWithRandomNum(int num) {
		String result = "";
		DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date d = new Date();
		result = sdf.format(d);
		String randomNum = RandomUtil.getRandomNumString(num);
		result = result + randomNum;
		return result;
	}

	/**
	 * 取当前时间到毫秒加上随机数
	 *
	 * @param num 17位加随机数位数
	 * @return
	 */
	public static String getMillisecondWithRandomNum(int num) {
		String result = "";
		DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date d = new Date();
		result = sdf.format(d);
		String randomNum = RandomUtil.getRandomNumString(num);
		result = result + randomNum;
		return result;
	}

	/**
	 * 得到一个随机的密码字符串
	 *
	 * @param length 返回字符串的长度
	 * @return 随即密码字符串
	 */
	public static final String getRandomString(int length) {
		Random randGen = null;
		char[] numbersAndLetters = null;

		if (length < 1) {
			return null;
		}
		if (randGen == null) {
			randGen = new Random();
			numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
					+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
		}
		char[] randBuffer = new char[length];
		for (int i = 0; i < randBuffer.length; i++) {
			randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
		}
		randGen = null;
		numbersAndLetters = null;
		return new String(randBuffer);
	}

	public static synchronized String getSerialId(String prefix) {

		Integer temp = new Double(Math.random() * 10000).intValue();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

		Long millis = System.currentTimeMillis();
		String tailLong = millis.toString();
		String tailtemp = temp.toString();
		String orderId = null;
		try {
			orderId = df.format(new Date())
					+ millis.toString().substring(tailLong.length() - 2, tailLong.length())
					+ tailtemp.substring(tailtemp.length() - 2, tailtemp.length());
		} catch (Exception e) {
			while (orderId == null) {
				orderId = df.format(new Date())
						+ millis.toString().substring(tailLong.length() - 2, tailLong.length())
						+ tailtemp.substring(tailtemp.length() - 2, tailtemp.length());
			}
		}
		return prefix + orderId;
	}

	public static void main(String[] args) {
		String  aa= RandomUtil.getMillisecondWithRandomNum(3);
		System.out.println(aa+"==length="+aa.length());
	}
}
